package com.fishery.processing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fishery.entity.GlobalException;
import com.fishery.entity.StatusCode;
import com.fishery.processing.dao.CraftDao;
import com.fishery.processing.dao.FactoryDao;
import com.fishery.processing.dao.InputDao;
import com.fishery.processing.dao.ProductDao;
import com.fishery.processing.entity.Craft;
import com.fishery.processing.entity.Factory;
import com.fishery.processing.entity.Input;
import com.fishery.processing.entity.Product;
import com.fishery.processing.entity.vo.CraftVo;
import com.fishery.processing.mapper.CraftMapper;
import com.fishery.processing.service.CraftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
@Service
public class CraftServiceImpl extends ServiceImpl<CraftMapper, Craft> implements CraftService {

    @Autowired
    private CraftDao craftDao;

    @Autowired
    private InputDao inputDao;

    @Autowired
    private FactoryDao factoryDao;

    public int createCraft(Craft craft){
        List<Craft> crafts = craftDao.findCraftByFactoryAndName(craft);
        if (!crafts.isEmpty()){
            throw new GlobalException(StatusCode.DUPLICATE,"该工艺已存在，无法添加");
        }
        Factory factory = factoryDao.findFactoryById(craft.getProcessingFactoryId());
        if (factory == null){
            throw new GlobalException(StatusCode.ERROR,"加工厂不存在，无法添加工艺");
        }
        //id置空，等待mybatis-plus填充
        craft.setId(null);
        return craftDao.addCraft(craft);
    }

    public Craft findCraftById(String craftId){
        return craftDao.findCraftById(craftId);
    }

    @Transactional
    public int deleteCraft(String craftId){

        //删除工艺与其投入品的连接信息
        inputDao.removeAllInputOfCraft(craftId);
        return craftDao.removeCraft(craftId);
    }

    public int updateCraft(Craft craft){
        List<Craft> crafts = craftDao.findCraftByFactoryAndName(craft);

        //如果存在该名称的工艺且id与要修改的工艺不相等
        if (!crafts.isEmpty() && !crafts.get(0).getId().equals(craft.getId())){
            throw new GlobalException(StatusCode.DUPLICATE,"该工艺名称已存在，无法修改");
        }

        return craftDao.updateCraft(craft);
    }

    public Craft searchCraftById(String craftId){
        return craftDao.findCraftById(craftId);
    }

    public int addInput(String craftId,String inputId){
        Input input = inputDao.findInputById(inputId);
        if (input == null){
            throw new GlobalException(StatusCode.ERROR,"该投入品不存在");
        }

        input = inputDao.findInputByCraftAndInput(craftId, inputId);
        if (input != null){
            throw new GlobalException(StatusCode.DUPLICATE,"工艺中已存在该投入品");
        }
        return inputDao.addInputOfCraft(craftId, inputId);
    }

    public int removeInput(String craftId,String inputId){
        return inputDao.removeInputOfCraft(craftId,inputId);
    }

    public IPage<Craft> searchCraftsOfProduct(String productId,Integer page,Integer size){
        return craftDao.findCraftsOfProduct(productId,page,size);
    }

    public List<Craft> searchCraftsOfProduct(String productId){
        return craftDao.findAllCraftsOfProduct(productId);
    }

    public IPage<Craft> searchCraftsOfFactory(String factoryId, Integer page, Integer size){
        return craftDao.findCraftsOfFactory(factoryId,page,size);
    }

    public List<Craft> searchCraftsOfFactory(String factoryId){
        return craftDao.findCraftsOfFactory(factoryId);
    }
}
